<template>
	<div class="router-view bcl-white">
		<!-- 搜索框 -->
		<div class="search-header box box-center bcl-active p-10">
			<div class="search-box box-1">
				<uni-search-box @onsubmit="onsubmit" @todata="todata"></uni-search-box>
			</div>

			<div class="box pl-10">
				<div class="mr-10" @click="toLinkPage">
					<image src="/static/images/icon_l.png" style="width: 20px; height: 21px;"></image>
				</div>
				<div @click="onsubmit">
					<image src="/static/images/icon_t.png" style="width: 20px; height: 21px;"></image>
				</div>
			</div>
		</div>

		<!-- 聊天列表 -->
		<div class="chat-list">
			<div class="item box container pointer" v-for="(item, index) in list" :key="index" @click="toChatPage(item.userProfile.userID)">
				<!-- 头像 -->
				<div class="avatar mr-10">
					<image style="display: block; width: 90upx; height: 90upx;" :src='(item.userProfile.avatar && item.userProfile.avatar.length > 0)?item.userProfile.avatar:avatar' />
				</div>

				<div class="info box-1 box box-column box-between">
					<div class="box box-between">
						<div class="name bold">{{item.userProfile.nick}}</div>
						<div class="fz-12 cl-second">{{item.lastMessage.lastTime | formatDate}}</div>
					</div>
					<div class="message">
						<span class="mr-5">[已读]</span>
						<span>{{item.lastMessage.messageForShow}}</span>
					</div>
				</div>
			</div>
		</div>

		<!-- 发布按钮 -->
		<div class="send-btn pointer" @click="toSendPage">
			<image src="/static/images/release_1.png" style="width: 50px; height: 50px;"></image>
		</div>
	</div>
</template>

<script>
	import uniSearchBox from "@/components/searchBox";
	import {
		mapState,
		mapActions
	} from "vuex";
	import initTim from "@/utils/initTim";
	// #ifdef H5
	import TIM from "tim-js-sdk";
	// #endif
	// #ifndef H5
	import TIM from "tim-wx-sdk";
	// #endif
	export default {
		data() {
			return {
				list: [],
				avatar: require("../../static/images/default.png"),
				todatas: '',
			};
		},
		onLoad() {
			this.initTim();
		},
		onTabItemTap(e){
		},
		created(){
		},
		onShow() {},
		filters: {
			formatDate: function(value) {
				value = value * 1000;
				let date = new Date(value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM;
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				let h = date.getHours();
				h = h < 10 ? ('0' + h) : h;
				let m = date.getMinutes();
				m = m < 10 ? ('0' + m) : m;
				let s = date.getSeconds();
				s = s < 10 ? ('0' + s) : s;
				return MM + '-' + d;
			}
		},

		methods: {

			toSendPage() {

				let url = uni.getStorageSync("userInfo")
				if (url) {
					uni.navigateTo({
						url: '/pages/index/release/submit?type=1'
					});

				} else {

					uni.navigateTo({
						url: '/user/login'
					});
				}
			},

			/*
			 * 初始化腾讯IM
			 */
			async initTim() {
				let tim = await initTim();
				this.SET_TIM(tim);
				this.listMessage();
			},

			/**
			 * 获取聊天列表
			 */
			async listMessage() {
				console.log("获取聊天列表1",this.tim);
				let {
					data
				} = await this.tim.getConversationList();
				console.log(data.conversationList[0].userProfile.avatar);
				this.list = data.conversationList;
			},

			toLinkPage() {
				uni.navigateTo({
					url: "/message/link",
				});
			},
			todata(value) {
				this.todatas = value.toString();
				console.log(this.todatas);
			},

			/*
			 * 快速发起会话
			 */
			onsubmit(user) {
				if (this.todata == '' || user == "") {
					alert("请输入用户id");
				}
				user = this.todata || user;
				let message = this.tim.createTextMessage({
					to: user,
					conversationType: TIM.TYPES.CONV_C2C,
					payload: {
						text: '您好'
					}
				});
				let promise = this.tim.sendMessage(message).then(function(imResponse) {
					// 发送成功
					console.log("发送成功", imResponse);
					uni.navigateTo({
						url: "/message/chat?toUserId=" + imResponse.data.message.to,
					});
				}).catch(function(imError) {
					// 发送失败
					alert(imError)
					console.warn('sendMessage error:', imError);
				});
			},


			...mapActions(["SET_TIM"]),
		},

		computed: {
			...mapState(["tim","userInfo"]),

		},

		components: {
			uniSearchBox,
		},
	};
</script>

<style lang="scss" scoped></style>
